// pages/order/payment/payment.js
const { httpPost, httpGet } = require('../../common/js/http')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      examId: options.examId,
      examPrice: options.examPrice,
      orderExamId: options.orderExamId,
      orderCode: options.orderCode
    })
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
 
  pay(){
    // wx.navigateTo({
    //   url: '../payResult/payResult?examId=' + this.data.examId
    // })
    // return
    app.getOpenid().then(res => {
      httpPost({
        url: "/app/payment/admin/getPaymentParams",
        data: {amount: this.data.examPrice,payType:0,paidFor:13,itemId: this.data.orderCode},
        isLoding: true,
        lodingMsg: '加载中...',
        header: { openid: app.globalData.openid }
      }).then(res => {
        console.log(res)
        if(res.errorCode == 0){
          wx.requestPayment({
            timeStamp: res.data.pay_timestamp+'',
            nonceStr: res.data.nonceStrPay,
            package: 'prepay_id='+res.data.prepayid,
            signType: res.data.signType,
            paySign: res.data.paySign,
            success: (res) => {
              wx.redirectTo({
                url: '../payResult/payResult?orderExamId=' + this.data.orderExamId
              })
            },
            fail: (err) => {
              console.log('err',err)
              wx.showToast({
                title: '取消支付成功！',
                icon: "success",
                duration: 1500,
              })
            }
          })
        }
      })
    })
  }
})